package com.serookie.controller;

import com.serookie.entity.Images;
import com.serookie.entity.Result;
import com.serookie.service.ImageService;
import com.serookie.utils.CosUtils;
import com.serookie.utils.DateFormatUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/5/5 22:12
 */
@RestController
@Api(value="广告操作接口",tags={"广告操作接口"})
@RequestMapping("/admin")
public class ImagesController{

    @Value("${tencent.path}")
    private String IMAGE_PATH;

    @Autowired
    private ImageService service;


    @ApiOperation("查询轮播图")
    @GetMapping("/swiper/{id}")
    public Result swiper(@PathVariable("id") Integer id){
        Images images = service.ImagesById(id);
        return Result.ok().message("查询成功").data("data",images);
    }

    @ApiOperation("删除轮播图")
    @PostMapping("/swiper/del/{id}")
    public Result deleteSwiper(@PathVariable("id")Integer id){
        Boolean flag = service.deleteImages(id);
        if(flag){
            return Result.ok().message("操作成功");
        }
        return Result.error().message("操作失败");
    }
    @ApiOperation("查询所有的轮播图")
    @GetMapping("/swiper/all")
    public Result findAll(){
        List<Images> all = service.findAll();
        return Result.ok().data("all",all).message("返回成功");
    }
    @ApiOperation("添加轮播图")
    @PostMapping("/swiper/add")
    public Result add(Images images){
        Boolean flag = service.saveImage(images);
        if(flag){
            return Result.ok().message("添加成功");
        }
        return Result.error().message("操作失败");
    }
//    上传轮播图(添加轮播图)
    @ApiOperation("上传轮播图")
    @RequestMapping(value = "/swiper/upload",method = RequestMethod.POST)
    public Result upload(@RequestParam("file") MultipartFile mulipartFilet) throws IOException {
        String fh="";
        String fileName = mulipartFilet.getOriginalFilename();
        //判断有无后缀
        if (fileName.lastIndexOf(".")<0) {
            return Result.ok().message("上传图片格式不正确!");
        }
        //获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));

        if(!prefix.equalsIgnoreCase(".jpg")&&!prefix.equalsIgnoreCase(".jpeg")
                && !prefix.equalsIgnoreCase(".svg")&&!prefix.equalsIgnoreCase(".png")){
            return Result.ok().message("上传格式不正确!");
        }
        //使用uuid作为文件名，防止生成的临时文件重复
        final File excelFile =File.createTempFile("imagesFile-" + System.currentTimeMillis(), prefix);
        //将Multifile转换成File
        mulipartFilet.transferTo(excelFile);

        //调用腾讯云工具上传文件
        String imageName = CosUtils.uploadfile(excelFile, "mall");
        return Result.ok().message("上传成功").data("data",IMAGE_PATH+imageName);
    }

    /**
     * 删除临时文件
     *
     * @param files 临时文件，可变参数
     */
    private void deleteFile(File... files) {
        for (File file : files) {
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
